package main

import (
	"fmt"
	"reflect"
	"strings"
)

type order struct {
	ordId      int
	customerId int
}

type employee struct {
	name    string
	id      int
	address string
	salary  int
	country string
}

const insertSqlTemplate = "insert into %s values (%s)"

func createQuery(q interface{}) {
	t := reflect.TypeOf(q)
	v := reflect.ValueOf(q)

	if t.Kind() != reflect.Struct {
		fmt.Println("unsupported type")
		return
	}

	var params []string
	for i := 0; i < v.NumField();i++ {
		field := v.Field(i)

		//pFile := fmt.Sprintf("field对应的i=%v，对应的value:%v", i,field)
		//fmt.Println("field :",field)
		//fmt.Println(pFile)

		switch field.Kind() {
		case reflect.Int:
			params = append(params, fmt.Sprintf("%v", field.Int()))
		case reflect.String:
			params = append(params, fmt.Sprintf("\"%v\"", field.String()))
		default:
			fmt.Println("unsupported type")
			return
		}
	}

	fmt.Println("params: ",params)
	fmt.Println("reflect.TypeOf: ",reflect.TypeOf(params))
	fmt.Println("strings.Join: ",strings.Join(params, ","))

	rowSql := fmt.Sprintf(insertSqlTemplate, v.Type().Name(), strings.Join(params, ","))
	fmt.Println(rowSql)
}
func main() {
	o := order{
		ordId:      456,
		customerId: 56,
	}
	createQuery(o)

	e := employee{
		name:    "Naveen",
		id:      565,
		address: "Coimbatore",
		salary:  90000,
		country: "India",
	}
	createQuery(e)
	i := 90
	createQuery(i)
}
